package com.example.music3;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager pager;
    private List<Integer>list_pic=new ArrayList<>();
    private TextView dao;
    private Button btn;
    private ArrayList<ImageView> criclelist;
    private LinearLayout li;
    private int time=6;
    @SuppressLint("HandlerLeak")
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what==2){
                --time;
                dao.setText("倒计时："+time+"S");
                if(time<=1){
                    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                    startActivity(intent);
                }
            }
        }
    };




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pager = (ViewPager) findViewById(R.id.pager);
        dao = (TextView) findViewById(R.id.dao);
        btn = (Button) findViewById(R.id.btn);
        li = (LinearLayout) findViewById(R.id.li);


        //添加图片
        list_pic.add(R.mipmap.a1);
        list_pic.add(R.mipmap.a2);
        list_pic.add(R.mipmap.aa);
        //添加进适配器
        picAdpater picAdpater = new picAdpater(list_pic, this);
        pager.setAdapter(picAdpater);
        criclelist = new ArrayList<>();
        for(int i = 0;i < list_pic.size();i++){
            ImageView imageView = new ImageView(this);
            if(i==0){
                imageView.setImageResource(R.drawable.cricle_t);
            }else {
                imageView.setImageResource(R.drawable.cricle);
            }
            criclelist.add(imageView);
            li.addView(imageView);

        }

        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for(int i = 0;i < criclelist.size();i++){
                    if(i==position){
                        criclelist.get(i).setImageResource(R.drawable.cricle_t);
                    }else {
                        criclelist.get(i).setImageResource(R.drawable.cricle);
                    }

                }
                if(position==2){
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            for(int i = 0;i <5 ;i++){
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                Log.i("TAG打印奥斯卡较好的卡拉是读后感拉萨", "run: "+i);
                                Message obtain = Message.obtain();
                                obtain.what=2;
                                handler.sendMessage(obtain);
                            }
                        }
                    }).start();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //按钮点击跳转
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                time=110;
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
            }
        });

    }
}
